#include <iostream>
#include <string>
#include <unordered_map>
#include <list>
using namespace std;
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
const int N = 2e5 + 10;
#define INF 0x3f3f3f3f;
typedef long long int ll;
#define close(); std::ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);
//----------------------------------------------------------------------------//
int n, m;
int main()
{
	close();

	cin >> n >> m;


	list<pair<int, int>> l;
	unordered_map<int, list<pair<int, int>>::iterator> mp;

	int num = n;
	int cnt = 1;
	while (num--)
	{

		l.push_back(make_pair(cnt++, 0));
	}


	cnt = 1;
	for (auto i = l.begin(); i != l.end(); ++i)
	{
		mp[cnt++] = i;
		//cout << "hehe" << '\n';
	}

	//cout<<"==========="<<'\n';
	while (m--)
	{
		int p, x;
		cin >> p >> x;
		if (p == 1)
		{
			pair<int, int> t = *l.begin();
			t.second += x;
			l.erase(l.begin());
			l.push_back(t);
			mp[t.first] = (--l.end());
		}
		if (p == 2)
		{
			auto cur = mp[x];
			pair<int, int> t = *cur;
			t.second++;
			l.erase(cur);
			l.push_back(t);
			mp[t.first] = (--l.end());

		}
		if (p == 3)
		{
			auto t = mp[x];
			if (t == l.begin()) cout << 0 << " ";
			else
			{
				pair<int, int> cur = *(--t);
				cout << cur.first << " ";
				t++;
			}
			
			if (t==(--l.end())) cout << n+1 << '\n';
			else
			{
				pair<int, int> cur = *(++t);
				cout << cur.first <<'\n';
			}
		}
		if (p == 4)
		{
			cout << (*mp[x]).second << '\n';
		}

	}



	return 0;
}
